package org.itboys.product.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.product.entity.ProductCategorySpec;
import org.itboys.product.service.ProductCategorySpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 商品分类下的规格特征
 * @author weisky
 *
 */
@Controller
@RequestMapping("/product/categoryspec")
public class ProductCategorySpecController extends BaseController{

	@Autowired
	private ProductCategorySpecService productCategorySpecService;
	
	@RequestMapping("/toCategorySpecVM")
	public String toCategorySpecVM(@RequestParam("categoryId") Long categoryId, Model model, HttpServletRequest request){
		model.addAttribute("categoryId", categoryId);
		return "/ace/product/categoryspec";
	}
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request,HttpServletResponse response){
		Page<ProductCategorySpec> page = productCategorySpecService.pageQuery(request);
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}
	
	@RequestMapping("/save")
	public void save(ProductCategorySpec productCategorySpec,
			HttpServletRequest request,HttpServletResponse response){
		boolean result = productCategorySpecService.insert(productCategorySpec);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	@RequestMapping("/getById")
	public void getById(@RequestParam("id") Long id,
			HttpServletRequest request, HttpServletResponse response){
		AjaxUtils.renderJson(response, productCategorySpecService.getById(id));
	}
	
	@RequestMapping("/delete")
	public void delete(@RequestParam("id") Long id,
			HttpServletRequest request,HttpServletResponse response){
		boolean result = productCategorySpecService.dele(id);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	/**
	 * 获取分类下的规格数
	 * @param categoryId
	 * @param response
     */
	@RequestMapping("/checkSize")
	public void checkSize(@RequestParam("categoryId")Long categoryId,
						  HttpServletResponse response){
		try{
			Long size = productCategorySpecService.checkSize(categoryId);
			AjaxUtils.renderText(response, size.toString());
		} catch (Exception e) {
			logger.error("/product/categoryspec/checkSize error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
}
